package org.zry.chat2ai.entity;

import org.springframework.ai.chat.messages.Message;
import org.springframework.ai.chat.messages.UserMessage;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

@Document(collection = "chat_history")
public class ChatHistory {
    @Id
    private String id;
    private String userId;
    private String sessionId;
    private List<String> userMessages;

    public ChatHistory() {
    }

    public ChatHistory(String userId, String sessionId, List<String> userMessages) {
        this.userId = userId;
        this.sessionId = sessionId;
        this.userMessages = userMessages;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public String getSessionId() {
        return sessionId;
    }

    public void setSessionId(String sessionId) {
        this.sessionId = sessionId;
    }

    public List<Message> getMessagesByString() {
        List<Message> messages = new ArrayList<>();
        if (!CollectionUtils.isEmpty(userMessages)) {
            for (String userMessage : userMessages) {
                messages.add(new UserMessage(userMessage));
            }
        }
        return messages;
    }

    public List<String> getUserMessages() {
        return userMessages;
    }

    public void setUserMessages(List<String> userMessages) {
        this.userMessages = userMessages;
    }

    @Override
    public String toString() {
        return "ChatHistory{" +
                "id='" + id + '\'' +
                ", userId='" + userId + '\'' +
                ", sessionId='" + sessionId + '\'' +
                ", messages=" + userMessages +
                '}';
    }
}